import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/views/layout/LayoutView.vue'
import Home from '@/views/home/HomeView.vue'
import Cart from '@/views/cart/CartView.vue'
import Shop from '@/views/shop/ShopView.vue'
import OrderConfirm from '@/views/order/OrderConfirmView.vue'
import Order from '@/views/order/OrderView.vue'
import Address from '@/views/address/AddressView.vue'
import My from '@/views/my/MyView.vue'
import LoginView from '@/views/login/LoginView.vue'
import RegisterView from '@/views/login/RegisterView.vue'
import CreateAddressView from '@/views/address/CreateAddressView.vue'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      name: 'layout',
      component: Layout,
      children:[
        {
          path:'/home',
          component:Home,
        },
        {
          path:'/cart',
          component:Cart
        },
        {
          path:'/order',
          component:Order
        },
        {
          path:'/my',
          component:My
        }
      ]
    },
    {
      path:'/shop',
      component:Shop,
    },
    {
      path:'/orderConfirm',
      component:OrderConfirm
    },
    ,
    {
      path:'/address',
      component:Address
    },
    {
      path:'/createAddress',
      component:CreateAddressView
    },
    {
      path:'/login',
      component:LoginView
    },
    {
      path:'/register',
      component:RegisterView
    },
    
  ]
})

//路由守卫
//前置
router.beforeEach((to,from,next)=>{
  if (to.path == '/login' || to.path == '/register') {
    next()
  }else{
    //不是登录或者注册页面
    if(sessionStorage.getItem('isLogin') == 'true'){
      //登录成功
      next()
    }else{
      //没有登录
      next({path:'/login'})
    }
  }
})

export default router
